<?php

namespace app\admin\controller;

use app\common\controller\Admin;

/**
 * 用户
 * Class User
 * @package app\admin\controller
 */
class User extends Admin
{

    /**
     * 会员首页
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $map = [];

        if (isset($param['uid']) && $param['uid']) {
            $map['id'] = $param['uid'];
        }

        if (isset($param['phone']) && $param['phone']) {
            $map['phone'] = $param['phone'];
        }

        $list = model('User')->relation('Money')->where($map)->order('id desc')
            ->paginate(15, false, array(
                'param' => $param
            ));

        $data = array(
            'list' => $list,
            'page' => $list->render(),
            'param' => $param
        );
        $this->assign($data);
        $this->setMeta('会员信息');
        return $this->fetch();
    }

    public function banks()
    {
        $param = $this->request->param();
        $map = [];
        if (isset($param['uid']) && $param['uid']) {
            $map['id'] = $param['uid'];
        }

        $list = model('UserBank')->where($map)->order('id desc')
            ->paginate(15, false, array(
                'param' => $param
            ));

        $data = array(
            'list' => $list,
            'page' => $list->render(),
            'param' => $param
        );

        $this->assign($data);
        $this->setMeta('会员信息');
        return $this->fetch();
    }

    /**
     * 编辑银行卡
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit_bank()
    {
        $model = model('UserBank');
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $reuslt = $model->where(['id' => $data['id']])->update($data);

            if (false !== $reuslt) {
                $this->success('修改成功！', url('user/banks'));
            } else {
                $this->error($model->getError(), '');
            }
        } else {
            $info = $model
                ->where(['id' => $this->request->param('id')])
                ->find();

            if (!$info) $this->error('用户不存在');

            $data = array(
                'info' => $info,
                'keyList' => $model->editfield,
            );
            $this->assign($data);
            $this->setMeta('编辑银行卡');
            return $this->fetch('public/edit');
        }

    }

    /**
     * 收货地址
     * @return mixed
     */
    public function address()
    {

        $param = $this->request->param();
        $map = [];

        if (isset($param['uid']) && $param['uid']) {
            $map['id'] = $param['uid'];
        }

//        if (isset($param['phone']) && $param['phone']) {
//            $map['phone'] = $param['phone'];
//        }

        $list = model('UserAddress')->where($map)->order('id desc')
            ->paginate(15, false, array(
                'param' => $param
            ));

        $data = array(
            'list' => $list,
            'page' => $list->render(),
            'param' => $param
        );
        $this->assign($data);

        $this->setMeta('收货地址');
        return $this->fetch();
    }


    /**
     * @title 修改用户
     * @author huajie <banhuajie@163.com>
     */
    public function edit()
    {
        $model = model('User');
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $reuslt = $model->editUser($data, true);

            if (false !== $reuslt) {
                $this->success('修改成功！', url('user/index'));
            } else {
                $this->error($model->getError(), '');
            }
        } else {
            $info = $model
                ->where(['id' => $this->request->param('id')])
                ->field('id,user_name,phone,status')
                ->find();

            if (!$info) $this->error('用户不存在');

            $data = array(
                'info' => $info,
                'keyList' => $model->editfield,
            );
            $this->assign($data);
            $this->setMeta("编辑用户");
            return $this->fetch('public/edit');
        }
    }

    /**
     * 修改收货地址
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit_address()
    {
        $model = model('UserAddress');
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $reuslt = $model->where(['id' => $data['id']])->update($data);

            if (false !== $reuslt) {
                $this->success('修改成功！', url('user/address'));
            } else {
                $this->error($model->getError(), '');
            }
        } else {
            $info = $model
                ->where(['id' => $this->request->param('id')])
                ->find();

            if (!$info) $this->error('用户不存在');

            $data = array(
                'info' => $info,
                'keyList' => $model->editfield,
            );
            $this->assign($data);
            $this->setMeta("编辑用户地址");
            return $this->fetch('public/edit');
        }
    }

}